package com.pinguix.games.pong;

import java.util.ArrayList;

import danger.ui.Pen;
import danger.ui.Point;
import danger.ui.Rect;

public class Paddle extends Object2D
{
	public static final int HEIGHT = 45;
	public static final int WIDTH = 7;
	public static final int DCONSTSPEED = 8;
	public static final int DSCROLLSPEED = 13;
	public static final int DECELLERATION = 3;
	
	private Rect _stageBounds;
	private int _constSpeed = DCONSTSPEED;
	private int _scrollSpeed = DSCROLLSPEED;
	private int _decelleration = DECELLERATION;
	

	/*
	 * Constructor with position of paddle only
	 */
	public Paddle(Point position)
	{
		this._bounds = new Rect(position.h, position.v, position.h + WIDTH, position.v + HEIGHT);
		this._velocity = new Point(0, 0);
		_stageBounds = null;
	}
	/*
	 * Constructor with position of paddle and bounds its contained in
	 */
	public Paddle(Point position, Rect stageBounds)
	{
		this._bounds = new Rect(position.h, position.v, position.h + WIDTH, position.v + HEIGHT);
		this._velocity = new Point(0, 0);
		_stageBounds = stageBounds;
	}

	public void paint(Pen pen)
	{
		pen.fillRect(_bounds);
	}
	
	//Updates position according to velocity and stage bounds
	public void update()
	{
		int accelleration = (int)(Math.signum(_velocity.v)*_decelleration);
		if(Math.signum(_velocity.v) == -1*Math.signum(_velocity.v - accelleration))
		{
			_velocity.v = 0;
			accelleration = 0;
		}
		_velocity.v -= accelleration;
		
		//Update position
		displaceY(_velocity.v);
		
		//Check for stage collision
		restrictToStage();
	}
	public Point getVelocity()
	{
		return _velocity;
	}
	public void moveUpConstant()
	{
		_velocity.v = -_constSpeed;
		_decelleration = 0;
	}
	public void moveDownConstant()
	{
		_velocity.v = _constSpeed;
		_decelleration = 0;
	}
	
	public void moveUp()
	{
		_velocity.v = -_scrollSpeed;
	}
	public void moveDown()
	{
		_velocity.v = _scrollSpeed;
	}
	
	public void stopMoving()
	{
		_decelleration = DECELLERATION;
	}
	
	public void displaceY(int displacement)
	{
		_bounds.top += displacement;
		_bounds.bottom += displacement;
	}
	
	//Determine an action to take when a collision occurs
	//Returns whether a collision occurred or not
	private void restrictToStage()
	{
		if(_stageBounds != null)
		{
			int height = _bounds.getHeight();
			if(_bounds.top < _stageBounds.top)
			{
				_bounds.top = _stageBounds.top;
				_bounds.bottom = _bounds.top + height;
			}
			else if(_bounds.bottom > _stageBounds.bottom)
			{
				_bounds.bottom = _stageBounds.bottom;
				_bounds.top = _stageBounds.bottom - height;
			}
		}
	}
	
	
	/*
	 * ----BEGIN GET/SET METHODS----
	 */
	
	//Sets the stage the paddle is contained in
	public void setStage(Rect bounds)
	{
		_stageBounds = bounds;
	}
	
	//Sets the available speed of the paddle
	public void setMaxSpeed(int speed)
	{
		_constSpeed = speed;
	}
	public int getMaxSpeed()
	{
		return(_constSpeed);
	}
	
	public Rect getBounds()
	{
		return _bounds;
	}
	public void setHeight(int height)
	{
		_bounds.bottom = _bounds.top + height;
	}
}
